package review;

import java.io.IOException;
import java.net.*;
import java.util.Scanner;

/**
 * Creater with IntelliJ IDEA.
 * Description:
 * User: 86152
 * Dae: 2022-08-05
 * Time: 19:52
 */
public class EchoClient {
    private DatagramSocket socket = null;
    public EchoClient() throws SocketException {
        socket=new DatagramSocket();
    }
    public void start() throws IOException {
        while(true) {
            //1.写请求
            Scanner scanner = new Scanner(System.in);
            String request = scanner.next();
            //2.将请求弄成packet
            DatagramPacket requestPacket = new DatagramPacket(request.getBytes(),0,request.getBytes().length, InetAddress.getByName("127.0.0.1"),8000);
            //3.发送
            socket.send(requestPacket);
            //4.解析传回来的响应
            DatagramPacket responsePacket = new DatagramPacket(new byte[4096],4096);
            socket.receive(responsePacket);
            String response = new String(responsePacket.getData(),0, responsePacket.getLength());
            System.out.println("响应："+response);
        }
    }

    public static void main(String[] args) throws IOException {
        EchoClient client = new EchoClient();
        client.start();
    }
}
